#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
#include <QMediaPlayer>
#include <QAudioOutput>
#include<QGraphicsPixmapItem>

class Player;

class GameWindow : public QMainWindow
{
    Q_OBJECT

public:
    GameWindow(int characterIndex = 0, QWidget *parent = nullptr);
    ~GameWindow();

protected:
    void keyPressEvent(QKeyEvent *event) override;

private slots:
    void updateGame();

private:
    void initializeGame(int characterIndex);
    void cleanScene();
    void createPlayer(int index);
    void spawnObstacle();
    void spawnCoin();
    void checkCollision();
    void gameOver();
    void resetGame();

    QGraphicsScene *scene;
    QGraphicsView *view;
    Player *player;
    QTimer *timer;
    QMediaPlayer *bgmPlayer;
    QAudioOutput *bgmOutput;

    QVector<QPair<QString, QString>> characterResources;
    int score;
    int coins;
    bool isGameOver;
};

class Player : public QGraphicsPixmapItem
{
public:
    Player(const QString& normalImg, const QString& jumpImg, QGraphicsItem *parent = nullptr);
    void jump();
    void updatePosition();
    void resetCharacter();
    void loadPixmapSafe(const QString& path, QPixmap &target);

    bool isJumping;
    int velocity;
    int jumpCount;
    int maxJumpCount;

private:
    QPixmap normalPixmap;
    QPixmap jumpingPixmap;
};

#endif // GAMEWINDOW_H
